package com.qianclass.talklive.app.json;

import android.os.Handler;
import android.os.Message;

import com.qianclass.talklive.app.utils.Constant;
import com.qianclass.talklive.app.utils.DateTime;
import com.qianclass.talklive.app.utils.FileLogUtils;
import com.qianclass.talklive.app.utils.Info;
import com.qianclass.talklive.app.utils.LogUtil;
import com.qianclass.talklive.app.utils.StringEncrypt;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.io.File;

/**
 *图片上传
 */

public class upLoadPhotoService {

    private Handler uploadHandler;
    private HttpHandler httpHandler;

    //获取通信线程
    public HttpHandler getHttpHandler() {
        return httpHandler;
    }

    //用于发送消息
    public upLoadPhotoService(Handler handler) {
        this.uploadHandler = handler;
    }

    public void upLoad (final String path , String fileName, String UserID) {
        LogUtil.e("上传", "文件地址：" + path);
        LogUtil.e("上传", "文件名：" + fileName);
        DateTime dateTime = new DateTime();
        String time = dateTime.getDateTime();
        String VerificationKey = StringEncrypt.S1Encrypt(Info.MECHANISM_ID + "+" + time, Info.API_EncryptionKey);
        RequestParams params = new RequestParams();
        params.addBodyParameter("Version", Info.APIVERSION);
        params.addBodyParameter("UA", "Android");
        params.addBodyParameter("UserID", UserID);
        params.addBodyParameter("FileName", fileName);
        params.addBodyParameter("VerificationKey", VerificationKey);//机构+时间字符串（例如：”rQ2ym3Jfk3K+2016-11-01 08:30:00” ）。字符串使用 s-1 算法加密。
        // 传图片时，要写3个参数: imageFile：键名 ;  new File(path)：要上传的图片，path图片路径 ; image/jpg：上传图片的扩展名
        params.addBodyParameter("imageFile", new File(path), "image/jpg");
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000 * 50); //设置超时时间
        FileLogUtils.write("Image Upload: " + Constant.getFileUploadUrl() + "\t path: " + path + "\t filename: " + fileName);
        httpHandler = http.send(HttpRequest.HttpMethod.POST, Constant.getFileUploadUrl(), params, new RequestCallBack<String>() {

            @Override
            public void onStart() {//开始
                super.onStart();
                Message msg = new Message();
                msg.what = 0;
                uploadHandler.handleMessage(msg);
            }

            @Override
            public void onLoading(long total, long current, boolean isUploading) {//上传中
                super.onLoading(total, current, isUploading);
                LogUtil.e("上传", "文件大小：" + total + "； 当前上传："+ current);
                int numbar = (int) (current * 100 / total);
                Message msg = new Message();
                msg.arg1 = numbar;
                msg.what = 1;
                uploadHandler.handleMessage(msg);
            }


            @Override
            public void onCancelled() {//取消请求回调
                Message msg = new Message();
                msg.what = 2;
                uploadHandler.handleMessage(msg);
                super.onCancelled();

            }



            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String resultStr = responseInfo.result;
                LogUtil.e("上传", "上传成功：" + resultStr);
                Message msg = new Message();
                msg.obj = resultStr;
                msg.what = 3;
                uploadHandler.handleMessage(msg);

            }

            @Override
            public void onFailure(HttpException error, String msg) {
                LogUtil.e("上传", "上传失败：" + error.getExceptionCode() + ":" + msg);
                Message message = new Message();
                message.obj = msg;
                message.what = 4;
                uploadHandler.handleMessage(message);
            }
        });
    }
}
